package userInterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import model.Auction;
import model.Control;

/**
 * @author Igor
 */

public class CalendarView extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JDesktopPane jDesktopPane = null;
	private JLabel lblMonth = null;
	private JLabel lblSun = null;
	private JLabel lblTue = null;
	private JLabel lblWed = null;
	private JLabel lblThur = null;
	private JLabel lblFri = null;
	private JLabel lblSur = null;
	private JLabel lblMon = null;
	private JButton btnPrev = null;
	private AuctionButton[][] buttons;
	private JButton btnNext = null;
	private Border blackline;
	private Calendar calendar; //  @jve:decl-index=0:
	private int current = 0;
	private Control control;
	/**
	 * This method initializes jDesktopPane
	 *
	 * @return javax.swing.JDesktopPane
	 */
	private JDesktopPane getJDesktopPane() {
		if (jDesktopPane == null) {
			blackline = BorderFactory.createLineBorder(Color.black);
			lblMon = new JLabel();
			lblMon.setBounds(new Rectangle(131, 74, 75, 30));
			lblMon.setText("Monday");
			lblMon.setBorder(blackline);
			lblMon.setHorizontalAlignment(JLabel.CENTER);

			lblSur = new JLabel();
			lblSur.setBounds(new Rectangle(505, 74, 76, 30));
			lblSur.setText("Saturday");
			lblSur.setBorder(blackline);
			lblSur.setHorizontalAlignment(JLabel.CENTER);

			lblFri = new JLabel();
			lblFri.setBounds(new Rectangle(431, 74, 75, 30));
			lblFri.setText("Friday");
			lblFri.setBorder(blackline);
			lblFri.setHorizontalAlignment(JLabel.CENTER);

			lblThur = new JLabel();
			lblThur.setBounds(new Rectangle(356, 74, 75, 30));
			lblThur.setText("Thursday");
			lblThur.setBorder(blackline);
			lblThur.setHorizontalAlignment(JLabel.CENTER);

			lblWed = new JLabel();
			lblWed.setBounds(new Rectangle(281, 74, 75, 30));
			lblWed.setText("Wednesday");
			lblWed.setBorder(blackline);
			lblWed.setHorizontalAlignment(JLabel.CENTER);

			lblTue = new JLabel();
			lblTue.setBounds(new Rectangle(206, 74, 75, 30));
			lblTue.setText("Tuesday");
			lblTue.setBorder(blackline);
			lblTue.setHorizontalAlignment(JLabel.CENTER);

			lblSun = new JLabel();
			lblSun.setBounds(new Rectangle(56, 74, 76, 30));
			lblSun.setText("Sunday");
			lblSun.setBorder(blackline);
			lblSun.setHorizontalAlignment(JLabel.CENTER);

			lblMonth = new JLabel();
			lblMonth.setBounds(new Rectangle(56, 44, 525, 30));
			lblMonth.setText("");
			lblMonth.setBorder(blackline);
			lblMonth.setHorizontalAlignment(JLabel.CENTER);

			jDesktopPane = new JDesktopPane();
			jDesktopPane.add(lblMonth, null);
			jDesktopPane.add(lblSun, null);
			jDesktopPane.add(lblTue, null);
			jDesktopPane.add(lblWed, null);
			jDesktopPane.add(lblThur, null);
			jDesktopPane.add(lblFri, null);
			jDesktopPane.add(lblSur, null);
			jDesktopPane.add(lblMon, null);
			buttons = new AuctionButton[6][7];
			int topLeftX = 56;
			int topleftY = 104;

			int x = topLeftX;
			int y = topleftY;
			for (int i = 0; i < 6; i++) {
				x = topLeftX;
				for (int j = 0; j < 7; j++) {
					buttons[i][j] = new AuctionButton(i + " " + j,null);
					buttons[i][j].setForeground(Color.BLACK);
					buttons[i][j].setBackground(Color.WHITE);
					Border line = new LineBorder(Color.BLACK);
					Border margin = new EmptyBorder(5, 15, 5, 15);
					Border compound = new CompoundBorder(line, margin);
					buttons[i][j].setBorder(compound);
					final AuctionButton btn = buttons[i][j];
					buttons[i][j].addActionListener(new java.awt.event.ActionListener() {

						public void actionPerformed(java.awt.event.ActionEvent e) {
//							JOptionPane.showMessageDialog(null, "Clicked!");
							if (btn.getAuction() == null){
								return;
							}
							AuctionView auctionView = new AuctionView(btn.getAuction(), control);
							auctionView.setLocationRelativeTo(null);
							auctionView.setVisible(true);
							dispose();
						}
					});
					buttons[i][j].setBounds(new Rectangle(x, y, 75, 50));
					jDesktopPane.add(buttons[i][j], null);
					x += 75;
				}
				y += 50;

			}
			jDesktopPane.add(getBtnPrev(), null);
			jDesktopPane.add(getBtnNext(), null);
		}
		return jDesktopPane;
	}

	/**
	 * This method initializes btnPrev
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnPrev() {
		if (btnPrev == null) {
			btnPrev = new JButton("<");
			btnPrev.setBounds(new Rectangle(8, 197, 43, 113));
			btnPrev.addActionListener(this);
		}
		return btnPrev;
	}

	/**
	 * This method initializes btnNext
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnNext() {
		if (btnNext == null) {
			btnNext = new JButton(">");
			btnNext.setBounds(new Rectangle(587, 197, 43, 113));
			btnNext.addActionListener(this);
		}
		return btnNext;
	}
	
	public CalendarView(final Control ctrl) {
		super();
		control = ctrl;
		initialize();

	}

	/**
	 * @author Major Bruno
	 */
	private void initialize() {
		this.setSize(640, 480);
		this.setContentPane(getJContentPane());
		this.setTitle("Calendar");

		calendar = Calendar.getInstance();
		displayCalendar();
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);

	}

	private void displayCalendar() {
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				buttons[i][j].setVisible(false);
			}
		}

		SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
		lblMonth.setText(sdf.format(calendar.getTime()));
		int maxDaysInMonth = calendar.getActualMaximum(Calendar.DATE);

		int x = 0;
		int y = 0;
		Calendar now = calendar;
		now.set(Calendar.DATE, 1);
		int startDate = now.get(Calendar.DAY_OF_WEEK);
		switch (startDate) {
		case Calendar.SUNDAY:
			y = 0;
			break;
		case Calendar.MONDAY:
			y = 1;
			break;
		case Calendar.TUESDAY:
			y = 2;
			break;
		case Calendar.WEDNESDAY:
			y = 3;
			break;
		case Calendar.THURSDAY:
			y = 4;
			break;
		case Calendar.FRIDAY:
			y = 5;
			break;
		case Calendar.SATURDAY:
			y = 6;
			break;
		}

		for (int i = 0; i < maxDaysInMonth; i++) {
			y++;
			if (y == 7) {
				y = 0;
				x++;
			}
			JButton button = buttons[x][y];
			button.setText(String.valueOf(i + 1));
			button.setVisible(true);
		}

		ArrayList<Auction> auctions = control.getAuctions();
		for (Auction auction: auctions)
		{
			Calendar auctionCal = auction.getDate();
			if (auctionCal.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) &&
					auctionCal.get(Calendar.YEAR) == calendar.get(Calendar.YEAR))
			{
				Point pos = getPosInArray(auctionCal.get(Calendar.DATE));
				buttons[pos.x][pos.y].setText(auction.getName());
				buttons[pos.x][pos.y].setAuction(auction);
			}
		}
	}
	private Point getPosInArray(int date)
	{
		int x = 0;
		int y = 0;
		Calendar now = calendar;
		now.set(Calendar.DATE, 1);
		int startDate = now.get(Calendar.DAY_OF_WEEK);
		switch (startDate) {
		case Calendar.SUNDAY:
			y = 0;
			break;
		case Calendar.MONDAY:
			y = 1;
			break;
		case Calendar.TUESDAY:
			y = 2;
			break;
		case Calendar.WEDNESDAY:
			y = 3;
			break;
		case Calendar.THURSDAY:
			y = 4;
			break;
		case Calendar.FRIDAY:
			y = 5;
			break;
		case Calendar.SATURDAY:
			y = 6;
			break;
		}
		for (int i = 0; i < date; i++) {
			y++;
			if (y == 7) {
				y = 0;
				x++;
			}
		}
		return new Point(x,y);
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	//tiny modification by Daniel Bartholomew
	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == btnPrev) {
			current--;

			btnNext.setEnabled(true);
			calendar.add(Calendar.MONTH, -1);
			displayCalendar();

		} else if (event.getSource() == btnNext) {
			btnPrev.setEnabled(true);
			current++;
			calendar.add(Calendar.MONTH, 1);
			displayCalendar();
			if (current == 3) {
				btnNext.setEnabled(false);
			}
		}
	}
}
